<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests\AdminLoginPostRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
	/*
	显示后台登录页面
	 */
	public function login()
	{
		//echo 1;
		//显示模板
		return view('admin.login.login');
	}
	/*
		后台登录验证
	 */
	public function check(AdminLoginPostRequest $request)
	{

		//dd($request->all());
		//检测用户是否存在
		$user = DB::table('user')->where('username',$request->input('username'))->first();
		
		//dd($user);
		// var_dump($request->input('password'));
		// var_dump($user->password);
		//var_dump(Hash::check($request->input('password'),$user->password));die;
				
		if ($user) {
			
		
			if(Hash::check($request->input('password'),$user->password)){
				//成功
				session(['id'=>$user->id]);
				//跳转到后台首页
				return redirect('/admin')->with('success','欢迎'.$user->username.'登录');
			}else{
				//失败
				return back()->with('error','用户名或密码错误');
			}
		}else{
			return back()->with('error','没有此用户名');
		}
	}
}